import {
  getLabelSort, putLabel, getLabel, postLabelSort, postLabel
} from '@/base/api/LabelApi';
import {deepAssign} from '@/base/util/Objects';
import {getDeviceSort} from '@/base/api/DeviceSortApi';
import {Page} from '@/base/Page';
import {getCompanyList} from '@/base/api/CompanyApi';
import {Company} from '@/base/model/Company';
import {DeviceSort} from '@/base/model/DeviceSort';
import {DeviceModel, DeviceModelName} from '@/base/model/DeviceModel';

export class EditBloc {
  // device: any = [];
  model: any = {
    labelId: 0 || undefined,
    companyId: 0,
    labelsortId:0 || undefined,
    labelname: '',
    sort: 0,
  };
  loading = false;
  num: number = 0;
  sortList: any = [];
  LabelName:any=[];
  companyList: any = []
  sortId: number = 0;
  // modelId: number = 0;
  isDetail = false;
  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getLabelSort({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = {labelsortId: 0, labelsortname: ''};
        // @ts-ignore
        obj.labelsortId = item.labelsortId;
        obj.labelsortname = item.labelsortname;
        return obj
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getCompanyList(pageIndex: number,
                 pageSize: number,
                 sortDirection: string) {
    return getCompanyList({pageIndex, pageSize, sortDirection, name: '', createBegin: ''})
      .then((res) => {
        deepAssign(this.company, res.data)
        this.companyList = res.data.items.map(item => {
          let obj = {companyId: 0, companyName: ''}
          obj.companyId = item.id;
          obj.companyName = item.name
          return obj
        })
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }

  addLabel(model) {
    this.loading = true;
    return postLabel({
      companyId: model.companyId,
      labelname: model.labelname,
      labelsortId: model.labelsortId
    }).then((res) => {
      // console.log(res);
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  getLabel(labelId: number) {
    this.loading = true;
    return getLabel({labelId}).then((res) => {
      deepAssign(this.model, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  //
  // LabelName: Array<any> = []
  //
  // putDeviceModelGather() {
  //   return putDeviceModelName().then((res) => {
  //     this.LabelName = res.data
  //   })
  // }

  editLabel(model: any) {
    return putLabel({
      labelId: model.labelId,
      companyId: model.companyId,
      labelsortId: model.labelsortId,
      labelname: model.labelname,
      sort: model.sort
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

}
